<template>
  <!--  时间选择-->
  <div class="block">
    <el-tooltip class="item" effect="dark" content="事件发生的时间范围" placement="top">
      <el-date-picker
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd"
          v-model="time"
          @change="timeOut"
          type="daterange"
          align="left"
          unlink-panels
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :picker-options="pickerOptions">
      </el-date-picker>
    </el-tooltip>
  </div>

</template>

<script>
export default {
  name: "timeSelect",
  data() {
    return {
      time:["",""],  //时间
      timeUnity: '',


      pickerOptions: {
        disabledDate(time) {
          return time.getTime() > Date.now();
        },
        timeDefaultShow: '',
        shortcuts: [{
          text: '今日',
          onClick(picker) {
            const end = new Date();
            end.setTime(end.getTime());
            end.setHours(end.getHours());
            end.setMinutes(end.getMinutes());
            end.setSeconds(end.getSeconds());
            const start = new Date();
            start.setTime(start.getTime());
            start.setHours(0);
            start.setMinutes(0);
            start.setSeconds(0);
            picker.$emit('pick', [start, end]);
          },
        }, {
          text: '昨日',
          onClick(picker) {
            const end = new Date();
            end.setTime(end.getTime() - 3600 * 1000 * 24 * 1);
            end.setHours(23);
            end.setMinutes(59);
            end.setSeconds(59);
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 1);
            start.setHours(0);
            start.setMinutes(0);
            start.setSeconds(0);
            picker.$emit('pick', [start, end]);
          },
        },
          {
            text: "本周",
            onClick(picker) {
              let end = new Date();
              let week = end.getDay();
              //一天的毫秒数
              let millisecond = 1000 * 60 * 60 * 24;
              //减去的天数
              let minusDay = week != 0 ? week - 1 : 6;
              //本周 周一
              let monday = new Date(end.getTime() - minusDay * millisecond);
              const start = new Date(monday);
              start.setHours(0, 0, 0, 0);
              console.log("2312", start, end);
              picker.$emit("pick", [start, end]);
            }
          }, {
            text: "上周",
            onClick(picker) {
              // 上周开始时间
              const starta = new Date();
              let first = starta.getDate() - starta.getDay() - 6;
              let startDate = new Date(starta.setDate(first));
              const start = new Date(startDate);
              start.setHours(0, 0, 0, 0);
              //  上周结束时间
              let currentDate = new Date();
              let firsts = currentDate.getDate() - currentDate.getDay() - 6;
              let last = firsts + 6;
              let endDate = new Date(currentDate.setDate(last));
              const end = new Date(endDate);
              end.setHours(23, 59, 59, 0);
              picker.$emit("pick", [start, end]);
            },
          }, {
            text: "本月",
            onClick(picker) {
              let currentDate = new Date();
              let currentMonth = currentDate.getMonth();
              //获得当前年份4位年
              let currentYear = currentDate.getFullYear();
              //求出本月第一天
              const start = new Date(currentYear, currentMonth, 1);
              const end = new Date();
              picker.$emit("pick", [start, end]);
            },
          }, {
            text: "上月",
            onClick(picker) {
              let that = this;
              //获取当前时间
              let currentDate = new Date();
              //获得当前月份0-11
              let currentMonth = currentDate.getMonth();
              //获得当前年份4位年
              let currentYear = currentDate.getFullYear();
              //获得上一个月的第一天
              if (currentMonth == 0) {
                currentMonth = 11; //月份为上年的最后月份
                currentYear--; //年份减1
                return new Date(currentYear, currentMonth, 1);
              } else {
                //否则,只减去月份
                currentMonth--;
              }

              const start = new Date(currentYear, currentMonth, 1);

              //获取当前时间
              let currentDates = new Date();
              //获得当前月份0-11
              let currentMonths = currentDates.getMonth();
              //获得当前年份4位年
              let currentYears = currentDates.getFullYear();

              //当为12月的时候年份需要加1
              //月份需要更新为0 也就是下一年的第一个月
              if (currentMonths == 11) {
                currentYears++;
                currentMonths = 0; //就为
              } else {
                //否则只是月份增加,以便求的下一月的第一天
                currentMonths++;
              }

              //一天的毫秒数
              let millisecond = 1000 * 60 * 60 * 24;
              //求出上月的最后一天
              let aa = new Date();
              let thisMonth = aa.getMonth();
              //获得当前年份4位年
              let thisYear = aa.getFullYear();
              //求出本月第一天
              let firstDay = new Date(thisYear, thisMonth, 1);
              let lastDay = new Date(firstDay.getTime() - millisecond);
              const end = new Date(lastDay);
              end.setHours(23, 59, 59, 0);
              picker.$emit("pick", [start, end]);
            },
          }, {
            text: '本年',
            onClick(picker) {
              //获取当前时间
              let currentDate = new Date();
              //获得当前年份4位年
              let currentYear = currentDate.getFullYear();

              //本年第一天
              const start = new Date(currentYear, 0, 1);
              //本年最后一天
              const end = new Date(currentYear, 11, 31);
              console.log("2312", start, end);
              picker.$emit("pick", [start, end]);
            }
          }, {
            text: '去年',
            onClick(picker) {
              //获取当前时间
              let currentDate = new Date();
              //获得当前年份4位年
              let currentYear = currentDate.getFullYear() - 1;

              //本年第一天
              const start = new Date(currentYear, 0, 1);
              //本年最后一天
              const end = new Date(currentYear, 11, 31);
              console.log("2312", start, end);
              picker.$emit("pick", [start, end]);
            }
          }, {
            text: '过去7天',
            onClick(picker) {
              const end = new Date();
              end.setHours(23);
              end.setMinutes(59);
              end.setSeconds(59);
              const start = new Date();
              start.setHours(0);
              start.setMinutes(0);
              start.setSeconds(0);
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '过去14天',
            onClick(picker) {
              const end = new Date();
              end.setHours(23);
              end.setMinutes(59);
              end.setSeconds(59);
              const start = new Date();
              start.setHours(0);
              start.setMinutes(0);
              start.setSeconds(0);
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 14);
              picker.$emit('pick', [start, end]);
            }
          },
          {
            text: '过去30天',
            onClick(picker) {
              const end = new Date();
              end.setHours(23);
              end.setMinutes(59);
              end.setSeconds(59);
              const start = new Date();
              start.setHours(0);
              start.setMinutes(0);
              start.setSeconds(0);
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 29);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '过去60天',
            onClick(picker) {
              const end = new Date();
              end.setHours(23);
              end.setMinutes(59);
              end.setSeconds(59);
              const start = new Date();
              start.setHours(0);
              start.setMinutes(0);
              start.setSeconds(0);
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 60);
              picker.$emit('pick', [start, end]);
            }
          },
          {
            text: '过去90天',
            onClick(picker) {
              const end = new Date();
              end.setHours(23);
              end.setMinutes(59);
              end.setSeconds(59);
              const start = new Date();
              start.setHours(0);
              start.setMinutes(0);
              start.setSeconds(0);
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '过去180天',
            onClick(picker) {
              const end = new Date();
              end.setHours(23);
              end.setMinutes(59);
              end.setSeconds(59);
              const start = new Date();
              start.setHours(0);
              start.setMinutes(0);
              start.setSeconds(0);
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 180);
              picker.$emit('pick', [start, end]);
            }
          }]
      },
    }
  },
  created() {
    this.initYestedayShowTime();
    this.$emit("timeUnity", this.time)
  },
  methods: {
    timeOut(time) {
      this.$emit("timeUnity", time)
    },
    //设置昨日为默认显示时间
    initYestedayShowTime() {
      let timenow = new Date();
      timenow.setDate(timenow.getDate()); //设置天数 -1 天，昨天的日期
      let y = timenow.getFullYear();//年
      let m = timenow.getMonth() + 1;//月
      m = m < 10 ? '0' + m : m;
      let d = timenow.getDate();//日
      d = d < 10 ? ('0' + d) : d;
      let todayTime = y + '-' + m + '-' + d;//字符串拼接成自己想要的时间格式，现在是yyyy/mm/dd格式

      let beforeTime = new Date();
      beforeTime.setDate(timenow.getDate() - 2); //设置天数 -1 天，昨天的日期
      y = beforeTime.getFullYear() //设置天数 -1 天，昨天的日期
      m = beforeTime.getMonth() + 1;//月
      m = m < 10 ? '0' + m : m;
      d = beforeTime.getDate();//日
      d = d < 10 ? ('0' + d) : d;
      let beforeYesTime = y + '-' + m + '-' + d;//字符串拼接成自己想要的时间格式，现在是yyyy/mm/dd格式
      this.time = [beforeYesTime, todayTime];
    },
  }
}
</script>

<style scoped>

</style>